# -*- coding:utf8 -*-

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
from keras.layers.advanced_activations import *
from data_prepare.load_data import load_hdf5_data_train,load_hdf5_data_test
import matplotlib.pyplot as plt
from my_core_layer.PLRelu import *
import os
import Image
import csv

batch_size = 128
nb_classes = 40
nb_epoch = 200
data_augmentation = False

# shape of the image (SHAPE x SHAPE)
img_rows, img_cols = 64, 64

# the CIFAR10 images are RGB
img_channels = 3

print('ConVNet_submit_predict')

# the data, shuffled and split between tran and test sets
X_test = load_hdf5_data_test(dataset='/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/submit/testVec64/FS/UnWVec')

print('X_test shape:', X_test.shape)
print(X_test.shape[0], 'test samples')

model = Sequential()

model.add(Convolution2D(64, 3, 3, input_shape=(img_channels, img_rows, img_cols), init='he_normal'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3, init='he_normal'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Convolution2D(128, 3, 3, init='he_normal'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 3, 3, init='he_normal'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(2048, init='he_normal'))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.load_weights('/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/Weights/submit/64/Aug2.hdf5')

if not data_augmentation:
    print('using Relu activation')
    X_test = X_test.astype("float32")
    X_test /= 255

    Pred = model.predict_classes(X_test)

    answerFile = '/home/dell/wxm/Code/JD/answer/AugVGG_FS_UnW.csv'
    f = file(answerFile,'wb')
    csvwriter = csv.writer(f)

    #name_file = '/home/dell/wxm/Code/JD/testset_names.csv'
    #f_name = file(name_file,'wb')
    #csvwriter_name = csv.writer(f_name)

    names = []
    print Pred
    for img in os.listdir('/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/Proc/test_washed_FS_UnW'):
        names.append(img)
        #csvwriter_name.writerow([img])
    #f_name.close()

    if len(Pred) != len(names):
        print 'submit fail'
    print "Pred length: " + str(len(Pred))
    print "names Length:" + str(len(names))
    for i in range(len(Pred)):
        answer = [names[i] , str(Pred[i])]
        csvwriter.writerow(answer)
    f.close()
